import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Statement;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.text.DateFormatter;

public class ButtonListener implements ActionListener {

	int counter = 0;
	public static int total = 0;
	static ArrayList<String> orderArray = new ArrayList<String>();
	static ArrayList<String> finalOrderArray = new ArrayList<String>();

	@Override
	public void actionPerformed(ActionEvent e) {
		
		String libCard = TaskDatabase.libraryCard.getText();
		
		if (e.getActionCommand().equals("submit")) {

			// if the isbn exists
			
			
			
			try {
				if(Borrow.LibraryCardNumber(TaskDatabase.libraryCard.getText())==true){
					TaskDatabase.submit.setEnabled(true);
					TaskDatabase.search.setEnabled(true);
					TaskDatabase.add.setEnabled(true);
					//TaskDatabase.book.setEnabled(true);		
				}else{
					JOptionPane.showMessageDialog(new JFrame(), "Library Card Does Not Exist!", "Error",
					        JOptionPane.ERROR_MESSAGE);
					
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
int couter = 0; 
		if (e.getActionCommand().equals("search")) {
			
			
			try {
				if(Borrow.checkbook(TaskDatabase.isbn.getText() )== true){
					TaskDatabase.frame2.setVisible(true);}
				else{ JOptionPane.showMessageDialog(new JFrame(), "Book not found!", "Error",
				        JOptionPane.ERROR_MESSAGE);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		
		if (e.getActionCommand().equals("add")) {
			
			try {
				if(Borrow.checkCopyNumber(TaskDatabase.isbn.getText() )== true){
					/*
					DateFormat dateFormat = new SimpleDateFormat("yyMMdd");
					Date date = new Date();
					dateFormat.format(date); 
					  	
					SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
					Calendar c = Calendar.getInstance();
					c.setTime(date);
					
					c.add(Calendar.DATE,14);
					Date newDate = new Date(); 
					//newDate = sdf.format(c.getTime());
					c.setTime(newDate); 
					
					
					//(sdf.format(c.getTime())); */
					
					java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
					java.sql.Date newDate = new java.sql.Date(System.currentTimeMillis() + 1209600000);
					
					System.out.println(TaskDatabase.libraryCard.getText());
					
					
					String sql = "INSERT INTO `dateBorrow`(`ISBN`, `BorrowDate`, `ReturnDate`, `LibraryCardNumber`) VALUES ( ? , ? , ? , ?)";
					PreparedStatement pst = TaskDatabase.con2.prepareStatement(sql);
					pst.setString(1, TaskDatabase.isbn.getText());
					pst.setDate(2, date);
					pst.setDate(3, newDate);
					pst.setInt(4, Integer.parseInt(TaskDatabase.libraryCard.getText()));
					
					pst.execute();
/*					
					Statement stmt = TaskDatabase.con2.createStatement();
					stmt.executeUpdate("INSERT INTO `dateBorrow`(`ISBN`, `BorrowDate`, `ReturnDate`, `LibraryCardNumber`) " +
							"VALUES ("+TaskDatabase.isbn.getText()+","+date+","+newDate+","+TaskDatabase.libraryCard.getText()+")");
	*/				
					
					TaskDatabase.frame2.setVisible(true);
				TaskDatabase.book.setEnabled(true);}
				else{ JOptionPane.showMessageDialog(new JFrame(), "Book is currently full booked", "Sorry",
				        JOptionPane.ERROR_MESSAGE);
				//TaskDatabase.book.setEnabled(false); 
					
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			// whatever here then remove
			couter++; 
			TaskDatabase.frame2.setVisible(false);
			
		}

		if (e.getActionCommand().equals("book")) {
			
			
				
			
			//TaskDatabase.book.setEnabled(false); 
			int counter = 0;
			String street = "", zipcode = "", town="", name="", libraryCardNumber="";
			try {
				Statement stmt = TaskDatabase.con2.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT borrow.LibraryCardNumber, borrow.Name, borrow.Street, borrow.ZipCode, borrow.Town, books.Author, books.Title, books.Year, " +
						"dateBorrow.ReturnDate FROM `dateBorrow` JOIN `borrow` ON dateBorrow.LibraryCardNumber = borrow.LibraryCardNumber JOIN " +
						"`books` ON books.ISBN = dateBorrow.ISBN WHERE borrow.LibraryCardNumber = "+libCard+";");
				
				TaskDatabase.pnlR.removeAll();
				JLabel te = new JLabel ("**Books Rented To Your Account**");
				TaskDatabase.pnlR.add(te);
				
				
				while(rs.next()){
					
					counter++;
					  
					
					libraryCardNumber = rs.getString("LibraryCardNumber");
					name = rs.getString("Name");
					street = rs.getString("Street");
					zipcode = rs.getString("ZipCode");
					town = rs.getString("Town"); 
					String author = rs.getString("Author"); 
					String title = rs.getString("Title");
					int year = rs.getInt("Year");
					Date returnDate = rs.getDate("ReturnDate"); 
					
					
					
					
					
					//System.out.println(libraryCardNumber + name + author + title + year + returnDate);
					TaskDatabase.frame3.setTitle("Card Number: "+ libraryCardNumber + " | Name " + name); 
					JLabel tmep = new JLabel(title + ", "+ author + " " + year + " | Return Date: " + returnDate);
					TaskDatabase.pnlR.add(tmep);
					TaskDatabase.frame3.setVisible(true); 
					
										
				}
				JLabel tmep3 = new JLabel("User Details:");
				JLabel tmep2 = new JLabel("Card Number: "+ libraryCardNumber + " | Name: " + name + " | Street: " + street + " | ZipCode: " + zipcode + " | Town: " + town);
				TaskDatabase.pnlR.add(tmep3);
				TaskDatabase.pnlR.add(tmep2);
				
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
			
			
		}

		
		
		
		
		
	}

}


/*SELECT *
FROM `dateBorrow`
JOIN `Borrow` ON dateBorrow.LibraryCardNumber = Borrow.LibraryCardNumber
JOIN `books` ON books.ISBN = dateBorrow.ISBN
LIMIT 0 , 30*/